<template>
  <div>
    <el-card style="margin: 20px 0">
      <CategorySelect :show="scene != 0" @getCategoryId="getCategoryId" />
    </el-card>
    <el-card>
      <div v-show="scene === 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!cate3Id"
          @click="toAddSpu"
        >添加</el-button>
        <el-table style="width: 100%; margin-top: 10px" border :data="records">
          <el-table-column type="index" label="序号" align="center" width="80" />
          <el-table-column prop="spuName" label="spu名称" width="width" />
          <el-table-column prop="description" label="spu描述" width="width" />
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <HintButton
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              />
              <HintButton
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="toUpdateSpu(row)"
              />
              <HintButton
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu全部sku列表"
                @click="getSkuInfo(row)"
              />
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <HintButton
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                />
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[5, 10, 15]"
          :page-size="limit"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="getSpuList"
        />
      </div>
      <SpuForm v-show="scene === 1" ref="spu" @changeScene="changeScene" />
      <SkuForm v-show="scene === 2" ref="sku" @changeSkuScene="changeSkuScene" />
    </el-card>

    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table v-loading="loading" :data="skuList" border>
        <el-table-column prop="skuName" label="名称" width="150" />
        <el-table-column prop="price" label="价格" width="200" />
        <el-table-column prop="weight" label="重量" />
        <el-table-column prop="address" label="默认图片">
          <template slot-scope="{ row }">
            <img :src="row.skuDefaultImg" alt="" style="height: 100px; width: 100px">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from './SpuForm'
import SkuForm from './SkuForm'
export default {
  name: 'Spu',
  components: { SpuForm, SkuForm },
  data() {
    return {
      cate1Id: '',
      cate2Id: '',
      cate3Id: '',
      page: 1,
      limit: 5,
      total: 0,
      // spu列表数据
      records: [],
      // 场景：  0:展示spu列表，1:添加|修改spu，2:添加sku
      scene: 0,
      // 控制对话框的显示与隐藏
      dialogTableVisible: false,
      spu: {},
      skuList: [],
      loading: true
    }
  },
  methods: {
    // 获取Spu列表数据
    async getSpuList(pages = 1) {
      this.page = pages
      const { page, limit, cate3Id } = this
      const result = await this.$API.spu.reqSpuList(page, limit, cate3Id)
      if (result.code == 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    getCategoryId({ categoryId, level }) {
      if (level == 1) {
        this.cate2Id = ''
        this.cate3Id = ''
        this.cate1Id = categoryId
      } else if (level == 2) {
        this.cate3Id = ''
        this.cate2Id = categoryId
      } else {
        this.cate3Id = categoryId
        // 发请求获取平台属性
        this.getSpuList()
      }
    },
    // 分页器更改每页大小
    handleSizeChange(limit) {
      this.limit = limit
      this.getSpuList()
    },
    // 去添加SpuForm
    toAddSpu() {
      this.scene = 1
      this.$refs.spu.addSpuData(this.cate3Id)
    },
    // 去修改SpuForm
    toUpdateSpu(row) {
      this.scene = 1
      // 在父组件中可以通过$refs获取子组件的数据
      this.$refs.spu.initSpuForm(row)
    },
    // 删除spu按钮的回调
    async deleteSpu(row) {
      const result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code == 200) {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1)
      }
    },
    // 添加sku按钮的回调
    addSku(row) {
      this.scene = 2
      this.$refs.sku.getData(this.cate1Id, this.cate2Id, row)
    },
    // 自定义事件回调（spuForm）
    changeScene({ scene, flag }) {
      this.scene = scene
      if (flag == '修改') {
        this.getSpuList(this.page)
      } else {
        this.getSpuList()
      }
    },
    // 自定义事件回调（skuForm）
    changeSkuScene(scene) {
      this.scene = scene
    },
    // 查看sku按钮回调
    async getSkuInfo(spu) {
      this.dialogTableVisible = true
      this.spu = spu
      // 获取sku列表的数据
      const result = await this.$API.spu.reqSkuList(spu.id)
      if (result.code == 200) {
        this.skuList = result.data
        this.loading = false
      }
    },
    // 关闭对话框的回调
    close(done) {
      this.loading = true
      this.skuList = []
      done()
    }
  }
}
</script>

<style></style>
